﻿#!/usr/bin/env python
# -*- coding: GBK -*-
import urllib2
import urllib
import logging
import logging.handlers
import os
import os.path
import sys
import shutil

def getXml():
    for i in range(20):
        filename='/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/log/iservucLog_20110712/iserv_fjas2server2.log.'+str(20-i)
        log.info(filename)
        logFileObject=open(filename)
        xmldata=''
        serviceUrl='http://134.128.196.11:9081/iservuc/ServiceGate/SimpleXMLGate'
        for lineStr in logFileObject.readlines():
            if isinstance(lineStr,unicode):#遇到中文关键字，要把unicode类型转换为str类型.
                lineStr=lineStr.encode('GBK')
            if lineStr.find('com.telthink.link.pub.ParamXmlUtil')>-1 and lineStr.find('sendSurvey')>-1:
                inputXML=lineStr[lineStr.find('<'):]
                log.info(inputXML)
                f=urllib2.urlopen(serviceUrl,urllib.urlencode({'xmldata':inputXML}))
                log.info(f.read().lstrip())

def copyFile(key,oldVoiceFileNameMap,newVoiceFileNameMap):
    oldFileName=oldVoiceFileNameMap[key]
    newFileName=newVoiceFileNameMap[key]
    copyOldFileName=oldFileName.replace('VMS_20110818bak/VMS_20110818bak','record/oldRecord')
    copyNewFileName=newFileName.replace('new_voice_20110817','record/newRecord')
    if os.path.isdir(os.path.split(copyOldFileName)[0]):
        #print 'copy '+oldFileName+' to '+os.path.split(copyOldFileName)[0]
        shutil.copy(oldFileName,os.path.split(copyOldFileName)[0])
    else:
        #print 'makdir '+os.path.split(copyOldFileName)[0]
        os.makedirs(os.path.split(copyOldFileName)[0])
        shutil.copy(oldFileName,os.path.split(copyOldFileName)[0])

    if os.path.isdir(os.path.split(copyNewFileName)[0]):
        #print 'copy '+newFileName+' to '+os.path.split(copyNewFileName)[0]
        shutil.copy(newFileName,os.path.split(copyNewFileName)[0])
    else:
        #print 'makdir '+os.path.split(copyNewFileName)[0]
        os.makedirs(os.path.split(copyNewFileName)[0])
        shutil.copy(newFileName,os.path.split(copyNewFileName)[0])

if __name__ == '__main__':
    oldVoicePath='/home/websphere/VMS_20110818bak/VMS_20110818bak'
    newVoicePath='/home/websphere/new_voice_20110817'
    oldVoiceMap={}
    newVoiceMap={}
    oldVoiceFileNameMap={}
    newVoiceFileNameMap={}
    difrecord=open('difrecord.txt','w')
    notexist=open('notexist.txt','w')
    for root,dirnames,filenames in os.walk(oldVoicePath):
        for filename in filenames:
            if filename.find('.wav')>-1:
                aTemp,bTemp=os.path.split(root)
                aTemp,bTemp=os.path.split(aTemp)
                bTemp=bTemp[:-3]
                #生成语音格式D00254001
                oldVoiceMap['D00'+bTemp.zfill(3)+filename[:-4].zfill(3)]=os.stat(os.path.join(root,filename))
                oldVoiceFileNameMap['D00'+bTemp.zfill(3)+filename[:-4].zfill(3)]=os.path.join(root,filename)
    for root,dirnames,filenames in os.walk(newVoicePath):
        for filename in filenames:
            if filename.find('.wav')>-1:
                aTemp,bTemp=os.path.split(root)
                aTemp,bTemp=os.path.split(aTemp)
                bTemp=bTemp[:-3]
                #生成语音格式D00254001
                newVoiceMap['D00'+bTemp.zfill(3)+filename[:-4].zfill(3)]=os.stat(os.path.join(root,filename))
                newVoiceFileNameMap['D00'+bTemp.zfill(3)+filename[:-4].zfill(3)]=os.path.join(root,filename)
    keyList=oldVoiceMap.keys()
    keyList.sort()
    for key in keyList:
        oldRecordSize=round(float(oldVoiceMap[key].st_size)/8000,2)
        newRecordSize=round(float(oldVoiceMap[key].st_size)/8000,2)
        if newVoiceMap.has_key(key)==False:
            notexist.write('新的语音列表不存在语音文件名:'+key+'\r\n')
            #print '新的语音列表不存在语音文件名:'+key
            continue
        else:
            newRecordSize=round(float(newVoiceMap[key].st_size)/8000,2)
        if oldRecordSize<=10 and abs(oldRecordSize-newRecordSize)>1:
            difrecord.write('语音文件名:'+key+'，录音时长相差:'+str(abs(oldRecordSize-newRecordSize))+'s'+'\r\n')
            copyFile(key,oldVoiceFileNameMap,newVoiceFileNameMap)
            #print '语音文件名:'+key+'，录音时长相差:'+str(abs(oldRecordSize-newRecordSize))+'s'
        elif oldRecordSize>10 and abs(oldRecordSize-newRecordSize)>2:
            difrecord.write('语音文件名:'+key+'，录音时长相差:'+str(abs(oldRecordSize-newRecordSize))+'s'+'\r\n')
            copyFile(key,oldVoiceFileNameMap,newVoiceFileNameMap)
            #print '语音文件名:'+key+'，录音时长相差:'+str(abs(oldRecordSize-newRecordSize))+'s'
    difrecord.close()
    notexist.close()

